package com.fusion.lottery.auth.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fusion.lottery.auth.service.RoleService;
import com.fusion.lottery.entity.auth.Authorization;
import com.fusion.lottery.entity.auth.Role;
import com.fusion.utils.json.Msg;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * @author louis
 *
 */
@Api(description="角色操作接口")
@RestController
@RequestMapping(value = "/role")
public class RoleController {
	
	@Autowired
	private RoleService roleService;
	
	@ApiOperation(value="获取角色列表", notes="获取角色列表")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "start", value = "起始记录", required = false, dataType = "Long" ,paramType = "query"),
		@ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Long" ,paramType = "query")
	})
	@GetMapping
	public Msg list(
			@RequestParam(defaultValue = "20", required = false, value = "pageSize") int pageSize,
			@RequestParam(defaultValue = "0", required = false, value = "start") int start,
			Role role) {
		return roleService.list(role,start,pageSize);
	}
	
	@ApiOperation(value="获取角色信息", notes="获取角色信息")
	@ApiImplicitParam(name = "id", value = "角色id", required = true, dataType = "Long",paramType = "path")
	@GetMapping("/{id}")
    public Msg get(@PathVariable long id) {
        return roleService.get(id);
    }
    
	@ApiOperation(value="添加角色", notes="添加角色")
	@ApiImplicitParam(name = "role", value = "角色实体", required = true, dataType = "Role")
    @PostMapping
    public Msg save(@Valid @RequestBody Role role) {
		return roleService.add(role);
    }
    
	@ApiOperation(value="修改角色", notes="修改角色")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "角色id", required = true, dataType = "Integer",paramType = "path"),
		@ApiImplicitParam(name = "role", value = "角色实体", required = true, dataType = "Role")
	})
    @PutMapping("/{id}")
    public Msg update(@PathVariable int id, @RequestBody Role role) {
		role.setId(id);
		return roleService.update(role);
    }
	
	@ApiOperation(value="删除角色", notes="删除角色")
	@ApiImplicitParam(name = "id", value = "角色id", required = true, dataType = "Long",paramType = "path")
    @DeleteMapping("/{id}")
    public Msg delete(@PathVariable long id) {
		return roleService.delete(id);
    }
	
	@ApiOperation(value="查询角色权限", notes="查询角色权限")
	@ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataType = "Long",paramType = "path")
	@GetMapping("/{roleId}/permission")
    public Msg getPermission(@PathVariable long roleId) {
		return roleService.getPermission(roleId);
    }
	
	@ApiOperation(value="查询角色下的用户", notes="查询角色下的用户")
	@ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataType = "Long",paramType = "path")
    @GetMapping("/{roleId}/user")
    public Msg getUser(@PathVariable long roleId) {
		return roleService.getUser(roleId);
    }
	
	@ApiOperation(value="分配角色", notes="分配角色")
	@ApiImplicitParam(name = "authorization", value = "用户角色", required = true, dataType = "Authorization")
    @PostMapping("/grant")
    public Msg grantRole(@Valid @RequestBody Authorization authorization) {
		return roleService.grantRole(authorization);
    }
	
	@ApiOperation(value="回收角色", notes="回收角色")
	@ApiImplicitParam(name = "authorization", value = "用户角色", required = true, dataType = "Authorization")
    @PostMapping("/recover")
    public Msg recoverRole(@Valid @RequestBody Authorization authorization) {
		return roleService.recoverRole(authorization);
    }
    
	@ApiOperation(value="角色授权", notes="角色授权")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataType = "Long",paramType = "path"),
		@ApiImplicitParam(name = "sourceIds", value = "逗号分割的权限id", required = true, dataType = "Long")
	})
	@PostMapping("/{roleId}/authorize")
    public Msg grantPermission(@PathVariable long roleId,@RequestBody String sourceIds) {
		return roleService.grantPermission(roleId, sourceIds);
    }
}